package com.kh.pajx.sop.service.sys.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.sys.ISysDepartmentDao;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.domain.SysDepartment;
import com.kh.pajx.sop.service.sys.ISysDepartmentService;
import com.kh.pajx.sop.util.page.QueryHelper;

/**
 * 部门信息业务逻辑实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月19日 上午2:16:10
 */
@Service
@Transactional
public class SysDepartmentServiceImpl extends BaseServiceImpl<SysDepartment> implements ISysDepartmentService {

	@Resource
	ISysDepartmentDao sysDepartmentDao;
	
	/**
	 * 添加部门信息
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#save(java.lang.Object)
	 */
	@Override
	public void save(SysDepartment demo) {
		sysDepartmentDao.save(demo);
	}
	
	/**
	 * 修改部门信息
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#update(java.lang.Object)
	 */
	@Override
	public void update(SysDepartment demo) {
		sysDepartmentDao.update(demo);
	}
	
	@Override
	public void delete(String uuid) {
		sysDepartmentDao.delete(uuid);
	}
	
	/**
	 * 加载部门信息
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#getById(java.lang.Long)
	 */
	@Override
	public SysDepartment getById(String id) {
		return sysDepartmentDao.getById(id);
	}
	
	/**
	 * 查找所有部门信息
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#findAll()
	 */
	@Override
	public List<SysDepartment> findAll() {
		return sysDepartmentDao.findAll();
	}
	
	/**
	 * 分页展示部门信息
	 * @see com.kh.pajx.sop.base.BaseServiceImpl#getPageBean(int, int, com.kh.pajx.sop.util.page.QueryHelper)
	 */
	@Override
	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelper queryHelper) {
		return sysDepartmentDao.getPageBean(pageNum, pageSize, queryHelper);
	}
	
	/**
	 * 查询所有子级部门信息
	 * @see com.kh.pajx.sop.service.sys.ISysDepartmentService#findChildrenDepartment(java.lang.String)
	 */
	@Override
	public List<SysDepartment> findChildrenDepartment(String deptCode) {
		return sysDepartmentDao.findChildrenDepartment(deptCode);
	}
	
	/**
	 * 查询所有顶级部门信息
	 * @see com.kh.pajx.sop.service.sys.ISysDepartmentService#findTopDepartment()
	 */
	@Override
	public List<SysDepartment> findTopDepartment() {
		return sysDepartmentDao.findTopDepartment();
	}
	
	/**
	 * 获取部门编码
	 * @see com.kh.pajx.sop.service.sys.ISysDepartmentService#getDeptCode()
	 */
	@Override
	public String getDeptCode() {
		String deptCode = sysDepartmentDao.getMaxDeptCode();
		if (StringUtils.isNotEmpty(deptCode)) {
			return (Integer.parseInt(deptCode) + 1) + "";
		} else{
			return "1000";
		}
		/*if(StringUtils.isEmpty(parentDeptCode)){
			String deptCode = sysDepartmentDao.getMaxDeptCode();
			if (StringUtils.isNotEmpty(deptCode)) {
				return (Integer.parseInt(deptCode) + 100) + "";
			} else{
				return "1000";
			}
		} else {
			String deptCode = sysDepartmentDao.getMaxDeptCode(parentDeptCode);
			if (StringUtils.isNotEmpty(deptCode)) {
				return (Integer.parseInt(deptCode) + 1) + "";
			} else{
				return (Integer.parseInt(parentDeptCode) + 1) + "";
			}
		}*/
	}
	
	/**
	 * 获取部门信息树状展示JSON数据
	 * @see com.kh.pajx.sop.service.sys.ISysDepartmentService#getDeptTreeJson()
	 */
	@Override
	public String getDeptTreeJson() {
		List<SysDepartment> departmentList = findAll();
		String jsonStr = "";
		for (int i = 0; i < departmentList.size(); i++) {
			if(i>0){
				jsonStr += ",";
			}
			/*jsonStr += "{\"deptCode\":\""+departmentList.get(i).getDeptCode()+"\""
					+",\"pCode\":\""+(departmentList.get(i).getParent() == null?"":departmentList.get(i).getParent().getDeptCode())+"\""
					+",\"deptName\":\""+departmentList.get(i).getDeptName()+"\"}";
			 */
			jsonStr += "{'treeId':'"+departmentList.get(i).getDeptCode()+"'"
					+",'pId':'"+(departmentList.get(i).getParent() == null?"":departmentList.get(i).getParent().getDeptCode())+"'"
					+",'treeName':'"+departmentList.get(i).getDeptName()+"'}";
		}
		return "[" + jsonStr + "]";
	}
	
	/**
	 * 按格式生成部门列表
	 * @see com.kh.pajx.sop.service.sys.ISysDepartmentService#getAll()
	 */
	@Override
	public List<SysDepartment> getAll(){
		List<SysDepartment> list = new ArrayList<SysDepartment>();
		walkDepartmentTrees(findTopDepartment(), "┣", list);
		return list;
	}
	
	
	/**
	 * 遍历部门树，把遍历出来的部门都放到指定的集合中
	 * @param topList
	 * @param prefix
	 * @param list   
	 * @author:	azzcsimp
	 * @Createdate:	2014年7月19日 下午5:58:41
	 */
	private void walkDepartmentTrees(Collection<SysDepartment> topList, String prefix, List<SysDepartment> list) {
		for (SysDepartment top : topList) {
			// 顶点
			SysDepartment copy = new SysDepartment(); // 原对象是在Session中的对象，是持久化状态，所以要使用副本。
			BeanUtils.copyProperties(top, copy);
			copy.setDeptName(prefix + copy.getDeptName());
			list.add(copy);
			// 子树
			walkDepartmentTrees(top.getChildren(), "　" + prefix, list); // 使用的是全角的空格
		}
	}
	
	
	/**
	 * 根据编号查找对象
	 * @see com.kh.pajx.sop.service.sys.ISysDepartmentService#getByCode(java.lang.String)
	 */
	@Override
	public SysDepartment getByCode(String deptCode) {
		return sysDepartmentDao.getByCode(deptCode);
	}
}
